package tool.workflow.fsSoft

import dao._
import implicits.Implicits._
import play.api.i18n.Messages
import tool.Pojo._
import shared.Pojo._
import shared.pojo.WorkflowMission.{EachIntersectData, EachOplsdaData, FsData}
import shared.tool.DefaultPickle
import tool.plot.{ModelArg, Oplsda2D, PermuteResult, VPlot, ZScore}
import tool.workflow.fsSoft.AllFsSoft.FSSoftBT
import tool.{FeatureSelectionTool, FormTool, Tool}
import tool.softT.{DiffAnaSoftBT, OplsdaSoftBT}
import utils.Utils

import java.io.File
import scala.collection.immutable.SeqMap
import upickle.default._

/**
 * Created by yz on 3/12/2020
 */
object FSOplsda extends FSSoftBT with OplsdaSoftBT {

  def produceSubConfigFile(workspaceDir: File, data: EachOplsdaData) = {
    val subConfigFile = new File(workspaceDir, "calculate_config.json")
    DefaultPickle.write(data).toFile(subConfigFile)
  }

  override def produceInput(workspaceDir: File, resultDir: File, index: Int, parentNodeStr: String, id: String)(
    implicit dao: MyDao,messages: Messages): Unit = {
    val value = Utils.configFile2Json(workspaceDir.getParentFile)
    val data = value.transform(reader[FsData]).oplsda(index)
    val groupFile = new File(workspaceDir.getParentFile, "group.txt")
    if (data.group.isBlank) {
      groupFile.fileCopyToDir(workspaceDir)
    } else {
      val groupStr = data.group
      val groups = groupStr.mySplit(" vs ")
      groupFile.txtLines.lineSeqMap.filter { map =>
        groups.contains(map("ClassNote"))
      }.csvLines.toTxtFile(new File(workspaceDir, "group.txt"))
    }
    FeatureSelectionTool.produceSubInputFile(workspaceDir, parentNodeStr)
    produceSubConfigFile(workspaceDir, data)
  }

}
